package com.shandao.shangxiu.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.shandao.shangxiu.R;
import com.shandao.shangxiu.base.BaseActivity;
import com.shandao.shangxiu.interf.PopupMenuClickListener;
import com.shandao.shangxiu.popup.MyPopupPickPhoto;
import com.shandao.shangxiu.util.TakePhotoTools;
import com.shandao.shangxiu.widget.CircleImageView;
import com.shandao.shangxiu.widget.CleanableEditText;

import java.io.File;

/**
 * 注册登录密码页面
 * @author king
 */
public class SetupInformationActivity extends BaseActivity {

	private CircleImageView civ_user_header;// 用户头像
	private CleanableEditText et_user_nickname;//用户昵称
	private CleanableEditText et_user_id;//用户ID
	private RadioGroup rg_gender;//性别选择
	private Button btn_regist;
	private TextView tvBack;//返回按钮

	private String phone;//手机号码
	private String password;//登录密码
	private Bitmap photo;//头像
	private String nickName;//昵称
	private String id;
	private String gender = "1";

	@Override
	protected int getLayoutId() {
		return R.layout.activity_setup_information;
	}


	@Override
	public void initView() {
		civ_user_header = (CircleImageView) findViewById(R.id.civ_user_header);
		et_user_nickname = (CleanableEditText) findViewById(R.id.et_user_nickname);
		et_user_id = (CleanableEditText) findViewById(R.id.et_user_id);
		rg_gender = (RadioGroup) findViewById(R.id.rg_gender);
		btn_regist = (Button) findViewById(R.id.btn_regist);

		civ_user_header.setOnClickListener(this);
		btn_regist.setOnClickListener(this);
		rg_gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
				if (checkedId == R.id.male) {
					gender = "1";
				}else if(checkedId == R.id.female){
					gender = "0";
				}
			}
		});
	}

	@Override
	public void initData() {
		setToolbarTitle("个人资料");
		Bundle bundle = getIntent().getExtras();
		phone = bundle.getString("phone");
		password = bundle.getString("password");
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.btn_regist) {
			submit();
		}else if (id == R.id.civ_user_header){
			showPickPopupMenu();
		}
	}

	/**
	 * 显示选择照片方式
	 */
	public void showPickPopupMenu(){
		final MyPopupPickPhoto popup = new MyPopupPickPhoto(this);
		popup.setListener(new PopupMenuClickListener() {
			@Override
			public void onPopueMenuItemClick(int viewID,String password) {
				popup.dismiss();
				if(viewID == R.id.tv_graph){
					// 拍照
					TakePhotoTools.startCameraOrGallery(
							SetupInformationActivity.this,
							TakePhotoTools.CAMERA);
				}else if(viewID == R.id.tv_pick){
					// 从手机中选择
					TakePhotoTools.startCameraOrGallery(
							SetupInformationActivity.this,
							TakePhotoTools.ALBUM);
				}
			}
		});
		popup.showAtLocation(civ_user_header, Gravity.BOTTOM,0,0);
	}


	private void submit() {
		if (!isPrepareForNext()) {
			return;
		}
		// TODO 下一步
		showWaitDialog("正在注册..");
//		Api.register(TAG, phone, password, passwordConfirm, nickName,
//				checkCode, Imei, photo, new VolleyInterface<ResponseBase>() {
//					@Override
//					public void onSuccess(ResponseBase result) {
//						setResult(RESULT_OK);
//						AutoLogin();
//					}
//
//					@Override
//					public void onFailed(String errorrMsg) {
//						hideWaitDialog();
//						showToast(errorrMsg);
//					}
//				});
	}
	
	/**
	 * 注册后自动登录
	 */
	private void AutoLogin() {
//		Api.login(TAG, phone, password, new VolleyInterface<ResponseToken>() {
//			@Override
//			public void onSuccess(ResponseToken result) {
//				String token = result.result.accessToken;
//				if (token == null || StringUtils.isEmpty(token)) {
//					ToastUtils.show(result.msg);
//					return;
//				}
//				// 保存登录信息 用户名 用户密码 token
//				Utils.saveUserInfo(SetupInformationActivity.this, phone, password,
//						DateUtils.formatDate(new Date()), token);
//				getUserInfo();
//			}
//
//			@Override
//			public void onFailed(String errorrMsg) {
//				hideWaitDialog();
//				showToast("请去登录");
//			}
//		});
	}

	private boolean isPrepareForNext() {
		isPrepareForNet();
		nickName = et_user_nickname.getText().toString().trim();
		if (TextUtils.isEmpty(nickName)){
			showToast("请输入用户昵称");
			return false;
		}
		id = et_user_id.getText().toString().trim();
		if (TextUtils.isEmpty(id)){
			showToast("请输入用户ID");
			return false;
		}
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
				// 相机
				case TakePhotoTools.CAMERA:
					TakePhotoTools.startPhotoZoom(this,
							Uri.fromFile(new File(TakePhotoTools.fileName)),200);
					break;
				// 相册
				case TakePhotoTools.ALBUM:
					if (data != null) {
						TakePhotoTools.startPhotoZoom(this, data.getData(),200);
					}
					break;
				case TakePhotoTools.PHOTO_REQUEST_CUT:
					photo = BitmapFactory.decodeFile(TakePhotoTools.fileName);
					civ_user_header.setImageBitmap(photo);
					break;

				default:
					break;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
